from django.db import models

from utils.basemodel import basemodel


# Create your models here.

class nav(basemodel):
    name = models.CharField(max_length=20, verbose_name="导航名称", unique=True)
    url = models.CharField(max_length=200, verbose_name="导航链接", null=True, blank=True)
    postion = models.IntegerField(verbose_name="导航位置", default=0)
    ishttp = models.BooleanField(verbose_name="是否是http链接", default=False)

    class Meta:
        db_table = "nav"
        verbose_name = "导航"


class categroy(basemodel):
    name = models.CharField(max_length=50, unique=True, verbose_name="分类名")
    parent = models.ForeignKey("self", blank=True, null=True, on_delete=models.CASCADE, verbose_name="父级分类",
                               related_name="children")
    desc = models.CharField(max_length=200)
    ishome = models.BooleanField(default=False)
    url = models.CharField(max_length=200, null=True, blank=True)

    class Meta:
        db_table = "categroy"
        verbose_name = "分类表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class teacher(basemodel):
    headimg = models.CharField(max_length=200, verbose_name="头像", null=True, blank=True)
    position = models.CharField(max_length=20, verbose_name="职位", null=True, blank=True)
    intro = models.CharField(max_length=200, verbose_name="介绍", null=True, blank=True)
    name = models.CharField(max_length=20, verbose_name="显示名字", null=True, blank=True)

    class Meta:
        db_table = "teacher"
        verbose_name = "讲师表"

    def __str__(self):
        return self.name


class tag(models.Model):
    name = models.CharField(max_length=20, verbose_name="tag名称")

    class Meta:
        db_table = "tag"
        verbose_name = "标签表"

    def __str__(self):
        return self.name


# 新学了一个choice=(()),码值和它对应值的元组
class course(models.Model):
    title = models.CharField(max_length=200, default='', verbose_name="课程名称")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="课程原价")
    realprice = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="折后价格")
    students = models.IntegerField(default=0, verbose_name="报名学生人数")
    level = models.IntegerField(choices=((0, '初级'), (1, '中级'), (2, '高级')), default=0, verbose_name="课程等级")
    category = models.ForeignKey(categroy, on_delete=models.CASCADE, related_name="category", verbose_name="课程分类")
    teacher = models.ForeignKey(teacher, on_delete=models.CASCADE, related_name="teacher", verbose_name="课程讲师")
    nav = models.ForeignKey(nav, on_delete=models.CASCADE, related_name="nav", verbose_name="课程导航")
    intro = models.CharField(max_length=200, verbose_name="课程介绍", default='',)
    isfinish = models.BooleanField(default=False, verbose_name="是否完成")
    finishtime = models.DateTimeField(auto_now_add=False, verbose_name="完成时间")
    score = models.IntegerField(default=0, verbose_name="分数")
    detail = models.CharField(max_length=200, default='', verbose_name="课程详情")
    cover = models.CharField(max_length=200, null=True, blank=True, verbose_name="封面图片")
    vedio = models.CharField(max_length=200, verbose_name="课程视频")
    isfree = models.BooleanField(default=False, verbose_name="是否免费")

    class Meta:
        db_table = "course"
        verbose_name = "课程表"

    def __str__(self):
        return self.title


# 多对多可以用两个一对多表示
class CourseTag(models.Model):
    course = models.ForeignKey(course, on_delete=models.CASCADE, verbose_name="课程")
    tag = models.ForeignKey(tag, on_delete=models.CASCADE, verbose_name="标签")

    class Meta:
        db_table = "coursetag"
        verbose_name = "课程标签"

    def __str__(self):
        return self.course.title


class chapter(basemodel):
    name = models.CharField(max_length=100, verbose_name="章节名称")
    intro = models.CharField(max_length=200, verbose_name="章节介绍")
    course = models.ForeignKey('course', on_delete=models.CASCADE, related_name="course")
    totaltime = models.IntegerField(default=0)

    class Meta:
        db_table = "chapter"
        verbose_name = "课程章"

    def __str__(self):
        return self.name


class node(basemodel):
    title = models.CharField(max_length=100, verbose_name="节点名称")
    no = models.CharField(max_length=10, verbose_name="节点序号")
    chapter = models.ForeignKey('chapter', on_delete=models.CASCADE, related_name="node")
    url = models.CharField(max_length=200, verbose_name="节点视频")
    isfree = models.BooleanField(default=False, verbose_name="是否免费")
    priod = models.IntegerField(default=0)

    class Meta:
        db_table = "node"
        verbose_name = "课程节"

    def __str__(self):
        return self.title



